package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.CaseInfo;
import cn.wolfcode.crm.domain.CompensationInfo;
import cn.wolfcode.crm.domain.InsureInfo;
import cn.wolfcode.crm.mapper.CompensationInfoMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICompensationInfoService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class CompensationInfoServiceImpl implements ICompensationInfoService {
    @Autowired
    private CompensationInfoMapper compensationInfoMapper;

    public void save(CompensationInfo entity, Long caseInfoId) {
        Long insureId = compensationInfoMapper.selectInsureInfoIdByCaseInfoId(caseInfoId);
        InsureInfo info = new InsureInfo();
        info.setId(insureId);
        CaseInfo caseInfo = new CaseInfo();
        caseInfo.setId(caseInfoId);
        entity.setInsureInfo(info);
        entity.setCaseInfo(caseInfo);

        if (entity.getId() == null) {
            compensationInfoMapper.insert(entity);
        } else {
            compensationInfoMapper.updateByPrimaryKey(entity);
        }

    }


    public void delete(Long id) {
        compensationInfoMapper.deleteByPrimaryKey(id);
    }

    public CompensationInfo get(Long id) {
        return compensationInfoMapper.selectByPrimaryKey(id);
    }

    public List<CompensationInfo> listAll() {
        return compensationInfoMapper.selectAll();
    }

    public PageResult query(QueryObject qo) {
        int count = compensationInfoMapper.queryCount(qo);
        if (count == 0) {
            return new PageResult(0, Collections.emptyList());
        }
        List<CompensationInfo> compensationInfos = compensationInfoMapper.queryList(qo);
        return new PageResult(count, compensationInfos);
    }

    public void deleteByCaseInfoId(Long caseInfoId) {
        compensationInfoMapper.deleteByCaseInfoId(caseInfoId);
    }

    @Override
    public Long selectInsureInfoIdByCaseInfoId(Long caseInfoId) {
        return compensationInfoMapper.selectInsureInfoIdByCaseInfoId(caseInfoId);
    }

    public CompensationInfo selectById(Long id) {
        return compensationInfoMapper.selectByPrimaryKey(id);
    }

    public CaseInfo selectCaseInfoById(Long id) {
        return compensationInfoMapper.selectCaseInfoById(id);
    }
}
